﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace Djs.HW.Util.MsCommPort
{
	public class LogAnalyzerGraph : Control
	{
		public void ShowPacket(IrReceiver.ReceivedPacket packet)
		{
			this._Settings();
			this._Packet = packet;
			this.Invalidate();
		}
		/// <summary>
		/// Vizuální paket
		/// </summary>
		public IrReceiver.ReceivedPacket Packet { get { return this._Packet; } set { this._Packet = value; } }
		private IrReceiver.ReceivedPacket _Packet;
		private void _Settings()
		{
			this.BackColor = Color.AliceBlue;
			this.Resize += new EventHandler(LogAnalyzerGraph_Resize);
		}

		void LogAnalyzerGraph_Resize(object sender, EventArgs e)
		{
			this.Invalidate();
		}
		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			if (this._Packet != null)
			{
				this.PaintPacketNo(e);
				this.PaintPacketItems(e);
			}
		}
		protected void PaintPacketNo(PaintEventArgs e)
		{
			using (Brush brush = new SolidBrush(Color.LightPink))
			{
				string numb = this._Packet.PacketNo.ToString();
				e.Graphics.DrawString(numb, SystemFonts.MessageBoxFont, brush, new PointF(25F, 6F));

				string freq = "";
				switch (this._Packet.FrequencyCarrier)
				{
					case IrReceiver.CarrierFrequency.None:
						freq = "F=???";
						break;
					case IrReceiver.CarrierFrequency.F36KHz:
						freq = "F=36 kHz";
						break;
					case IrReceiver.CarrierFrequency.F38KHz:
						freq = "F=38 kHz";
						break;
				}
				e.Graphics.DrawString(freq, SystemFonts.MessageBoxFont, brush, new PointF(25F, 23F));
			}
		}
		protected void PaintPacketItems(PaintEventArgs e)
		{
			int x0 = 5;
			int y0 = this.Height - 5;
			int y1 = 5;
			Point point = new Point(x0, y0);
			decimal measure = 20000M;                   // Počet pixelů na jednu sekundu

			// Určím měřítko tak, abych zobrazil celý průběh:
			decimal timeTotal = this._Packet.TimeTotal;
			if (this._TimeMax > 0M && this._TimeMax > timeTotal)
				timeTotal = this._TimeMax;

			int graphWidth = this.Width - 25;
			if (timeTotal > 0M)
				measure = (decimal)graphWidth / timeTotal;  // pixel / sekunda

			// Vykreslit:
			decimal time0 = 0M;
			using (Pen pen = new Pen(Color.LightYellow, 2F))
			{
				foreach (IrReceiver.ReceivedItem item in this._Packet.PacketItems)
				{
					Point next;

					next = new Point(point.X, (item.Value ? y1 : y0));
					if (next != point)
						e.Graphics.DrawLine(pen, point, next);
					point = next;

					decimal timeE = time0 + item.Time;
					time0 = timeE;
					decimal xE = timeE * measure;          // Počet bodů za daný čas
					int x1 = x0 + (int)xE;
					next = new Point(x1, point.Y);

					if (next != point)
						e.Graphics.DrawLine(pen, point, next);
					point = next;

					if (point.X > this.Width)
						break;
				}

				// Poslední impuls by měl končit tam, kde první začal:
				if (point.X <= this.Width && point.Y != y0)
				{
					Point next = new Point(point.X, y0);
					e.Graphics.DrawLine(pen, point, next);
				}
			}
		}

		internal void DetectTimeMax(ref decimal timeMax)
		{
			if (this._Packet != null)
			{
				decimal timeTotal = this._Packet.TimeTotal;
				if (timeTotal > timeMax)
					timeMax = timeTotal;
			}
		}

		internal void SetTimeMax(decimal timeMax)
		{
			this._TimeMax = timeMax;
			this.Invalidate();
		}

		private decimal _TimeMax;
	}
}
